﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html class="x-admin-sm">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta charset="UTF-8">
    <title>创建盘点任务</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link href="~/Content/X-admin/css/font.css" rel="stylesheet" />
    <script src="~/Content/X-admin/lib/layui/layui.js"></script>
    <link href="~/Content/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/Content/X-admin/js/jquery.min.js"></script>
    <script src="~/Content/X-admin/js/xadmin.js"></script>
    <script src="~/Content/Scripts/System/systemFrame.js"></script>
    <script src="~/Content/Scripts/System/Common.js"></script>
</head>
<body>
    <span style="margin-top:15px;margin-left:15px">
        <xblock>
            <button class="layui-btn" onclick="saveData()"><i class="layui-icon"></i>保存</button>
            <button class="layui-btn" onclick="closeWin()"><i class="layui-icon">&#x1006;</i>取消</button>
        </xblock>
    </span>

    <div style="margin: 20px; border: 1px solid #dbdbdb;padding:15px">

        <form id="AddForm" class="layui-form layui-col-space5">
            <div class="layui-form-item">
                <div class="layui-inline layui-show-xs-block" style="width:150px ">
                    <input type="text" name="code" placeholder="盘点单号" autocomplete="off" class="layui-input" id="code" >
                </div>
                <div class="layui-inline layui-show-xs-block" style="width:150px ">
                    <input type="text" name="cWhName" placeholder="仓库" autocomplete="off" class="layui-input" id="cWhName">
                </div>
                <div class="layui-inline layui-show-xs-block" style="width:150px ">
                    <input type="text" name="cPosName" placeholder="货位" autocomplete="off" class="layui-input" id="cPosName">
                </div>
                <div class="layui-inline layui-show-xs-block" style="width:150px " >
                    <input type="text" name="cInvName" placeholder="存货名称" autocomplete="off" class="layui-input" id="cInvName">
                </div>
                <label for="L_email" class="layui-form-label">
                    盘点类别
                </label>
                <div class="layui-input-inline" style="width:150px ">
                    <select name="types" id="types" lay-filter="types" >
                    </select>
                </div>
                <div class="layui-inline layui-show-xs-block">
                    <button class="layui-btn" lay-filter="sreach" id="userSearch-view" type="button" onclick="search()"><i class="layui-icon">&#xe615;</i></button>
                </div>
            </div>

        </form>

        <div class="layui-card-body layui-table-body" id="layui-table-main">
            <table class="layui-hide" id="list" lay-filter="list"></table>
        </div>

    </div>
</body>
</html>
<script type="text/javascript">
    var list = [];
    var type = "pc-ware";//盘点类型
    var err = 0;

    $(function () {
        var dataId = getUrlVariable("dataId");
        DictionaryDropDown("ppct", "types", "");
        loadData("pc-ware")
    });

    layui.use(['layer', 'jquery', 'form'], function () {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form;
        form.on('select(types)', function (data) {
            var value = data.value;
            type = value;

            loadData(value)
        });

    });

    function search() {
        loadData(type)
    }

    //仓库盘点 获取盘点单主表
    function loadData(chtype) {
        layui.use('table', function () {
            var conditon = JSON.stringify(FormToJson("AddForm"));
            var table = layui.table;
            table.render({
                elem: '#list'
                , cols: [[ //标题栏
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'code', title: '盘点单号', width: 100 }
                    , { field: 'wareName', title: '盘点仓库', width: 130 }
                    , { field: 'positionName', title: '盘点货位', width: 110 }
                    , { field: 'proName', title: '盘点存货', width: 150 }
                    , { field: 'spec', title: '规格', width: 110 }
                    , { field: 'number', title: '数量', width: 100 }
                ]], done: function (res, curr, count) {
                    if (chtype == "pc-ware") {//仓库
                        $("[data-field='positionName']").css('display', 'none');//隐藏列
                        $("[data-field='proName']").css('display', 'none');//隐藏列
                        $("[data-field='spec']").css('display', 'none');//隐藏列

                        $("#cWhName").show();
                        $("#cPosName").hide();
                        //$("#cInvCode").hide();
                        $("#cInvName").hide();
                    }
                    else if (chtype == "pc-position") { //货位
                        $("[data-field='proName']").css('display', 'none');//隐藏列
                        $("[data-field='spec']").css('display', 'none');//隐藏列

                        $("#cWhName").show();
                        $("#cPosName").show();
                        //$("#cInvCode").hide();
                        $("#cInvName").hide();
                    }
                    else if (chtype == "pc-pospro") {//货位存货
                        $("#cPosName").show();
                        //$("#cInvCode").show();
                        $("#cInvName").show();
                    }
                }
                , url: "../ProCheck/GetDataList?dataType=erptasks&typesCode=" + type + "&condition=" + conditon
                , skin: 'row' //表格风格
                , even: true
                , page: false //是否显示分页
                , limits: [20, 30, 50]
                , limit: 20 //每页默认显示的数量
                , id: "tData"
            });

            //监听工具条
            table.on('checkbox(list)', function (obj) {
                var checkStatus = table.checkStatus('tData');
                var datas = checkStatus.data;
                err = 0;
                list = [];
                for (var i = 0; i < datas.length; i++) {
                    if (list.length > 0) {
                        if (datas[i].code != list[0]["erpCode"]) {
                            layer.msg("不支持多个盘点单同时盘点：" + list[0]["erpCode"] + "," + datas[i].code);
                            err = 1;
                            return;
                        }
                        if (datas[i].positionName != list[0]["positionName"]) {
                            layer.msg("不支持多个货位同时盘点：" + list[0]["positionName"] + "," + datas[i].positionName);
                            err = 1;
                            return;
                        }
                    }
                    var obj = new Object();
                    obj.erpCode = datas[i].code
                    obj.erpWareCode = datas[i].wareCode;
                    obj.erpPositionCode = datas[i].positionCode;
                    obj.positionName = datas[i].positionName;
                    obj.erpProCode = datas[i].proCode;
                    obj.proName = datas[i].proName;
                    obj.color = datas[i].color;
                    obj.shangbiao = datas[i].shangbiao;
                    obj.erpNumber = datas[i].number;
                    //obj.erpId = datas[i].erpId;
                    list.push(obj);
                }

            });
        });
    }

    // 保存数据
    function saveData() {
        if (!err) {
            if (list.length < 1) {
                layer.msg("请先选择盘点单"); return;
            }
            var loadwin = layer.load(2, {
                shade: [0.3, '#424242'],
                content: '生成中',
                success: function (layerContentStyle) {
                    layerContentStyle.find('.layui-layer-content').css({
                        'padding-top': '35px',
                        'text-align': 'left',
                        'width': '120px'
                    });
                }
            }, { shadeClose: true });
               

            var datas = JSON.stringify(list);

            $.ajax({
                url: '../ProCheck/CreateTask',
                data: { datas: datas, type: type },
                dataType: "json",
                type: "POST",
                success: function (data) {
                    layer.close(loadwin);
                    if (data.Result) {
                        layer.msg("盘点任务创建成功");
                        TaskTable();
                    } else {
                        layer.msg(data.Message);
                    }

                }
            });

        }
    }


    layui.use(['laydate'], function () {
        var $ = layui.$;
        var laydate = layui.laydate;
        var nowTime = new Date().valueOf();
        var max = null;
        var start = laydate.render({
            elem: '#rDatetime',
            trigger: 'click',
            type: 'datetime',
            format: 'yyyy/MM/dd',
            btns: ['clear', 'confirm'],

            done: function (value, date) {

            }
        });

    })

</script>